package cn.stylefeng.guns.modular.pandian.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.basic.entity.BasicArea;
import cn.stylefeng.guns.modular.basic.model.params.BasicAreaParam;
import cn.stylefeng.guns.modular.basic.model.result.BasicAreaResult;
import cn.stylefeng.guns.modular.basic.service.BasicAreaService;
import cn.stylefeng.guns.modular.pandian.entity.PandianJihua;
import cn.stylefeng.guns.modular.pandian.entity.PandianJihuaUser;
import cn.stylefeng.guns.modular.pandian.model.params.PandianJihuaUserParam;
import cn.stylefeng.guns.modular.pandian.model.result.PandianJihuaUserResult;
import cn.stylefeng.guns.modular.pandian.service.PandianJihuaService;
import cn.stylefeng.guns.modular.pandian.service.PandianJihuaUserService;
import cn.stylefeng.guns.modular.storage.service.StorageStockService;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.model.UserDto;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;


/**
 * 盘点人员表控制器
 */
@Controller
@RequestMapping("/pandianJihuaUser")
public class PandianJihuaUserController extends BaseController {

    private String PREFIX = "/pandian/pandianJihuaUser";

    @Autowired
    private PandianJihuaService pandianJihuaService;

    @Autowired
    private PandianJihuaUserService pandianJihuaUserService;

    @Autowired
    private UserService userService;

    @Autowired
    private BasicAreaService basicAreaService;

    /**
     * 跳转到主页面
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/pandianJihuaUser.html";
    }

    /**
     * 新增页面
     */
    @RequestMapping("/add")
    public String add(PandianJihuaUserParam pandianJihuaUserParam,Model model) {
    	User user = userService.getcurrentUser();
    	BasicAreaParam basicAreaParam = new BasicAreaParam();
    	basicAreaParam.setAreaWarehouseId(Integer.parseInt(pandianJihuaUserParam.getWarehouseId().toString()));
    	List<BasicAreaResult> alist = basicAreaService.findListBySpec(basicAreaParam, null,null);
    	model.addAttribute("alist",alist);
    	UserDto userDto = new UserDto();
    	userDto.setDeptId(user.getDeptId());
    	userDto.setDeliverId(user.getDeliverId());
    	List<UserDto> ulist = userService.findListBySpec(userDto,user.getOperateId());
    	model.addAttribute("ulist",ulist);
        return PREFIX + "/pandianJihuaUser_add.html";
    }

    /**
     * 编辑页面
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/pandianJihuaUser_edit.html";
    }

    /**
     * 新增接口
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(PandianJihuaUserParam pandianJihuaUserParam) {
    	PandianJihua pandianJihua = pandianJihuaService.getById(pandianJihuaUserParam.getDiskId());
    	if(pandianJihua.getStatus().equals("已结束")) {
    		return ResponseData.error("该盘点计划已结束！");
    	}
    	if(pandianJihua.getStatus().equals("进行中")) {
    		return ResponseData.error("该盘点计划已开始！");
    	}
    	pandianJihuaUserParam.setDiskName(pandianJihua.getDiskName());
    	
    	PandianJihuaUserParam param = new PandianJihuaUserParam();
    	param.setDiskId(pandianJihuaUserParam.getDiskId());
    	param.setAreaId(pandianJihuaUserParam.getAreaId());
    	param.setUserId(pandianJihuaUserParam.getUserId());
    	PandianJihuaUserResult pandianJihuaUserResult = pandianJihuaUserService.findBySpec(param);
    	if(pandianJihuaUserResult != null) {
    		return ResponseData.error("请勿重复添加人员");
    	}
    	
    	User user = userService.getcurrentUser();

    	pandianJihuaUserParam.setSpeed("0");
    	pandianJihuaUserParam.setCountNum("0");
    	pandianJihuaUserParam.setAlreadyNum("0");
    	pandianJihuaUserParam.setAlreadyVolume("0");
    	pandianJihuaUserParam.setCountVolume("0");
    	pandianJihuaUserParam.setTrueNum("0");
    	pandianJihuaUserParam.setTrueVolume("0");
    	
    	BasicArea basicArea = basicAreaService.getById(pandianJihuaUserParam.getAreaId());
    	pandianJihuaUserParam.setWarehouseId(Long.parseLong(basicArea.getAreaWarehouseId().toString()));
    	pandianJihuaUserParam.setWarehouseName(basicArea.getAreaWarehouseName());
    	pandianJihuaUserParam.setAreaName(basicArea.getAreaName());
    	
    	User user2 = userService.getById(pandianJihuaUserParam.getUserId());
    	pandianJihuaUserParam.setUserName(user2.getName());
    	pandianJihuaUserParam.setUserAccount(user2.getAccount());
    	
    	pandianJihuaUserParam.setDeptId(user.getDeptId());
    	pandianJihuaUserParam.setDeliverId(user.getDeliverId());
        this.pandianJihuaUserService.add(pandianJihuaUserParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(PandianJihuaUserParam pandianJihuaUserParam) {
        this.pandianJihuaUserService.update(pandianJihuaUserParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(PandianJihuaUserParam pandianJihuaUserParam) {
        PandianJihuaUser detail = this.pandianJihuaUserService.getById(pandianJihuaUserParam.getId());
    	PandianJihua pandianJihua = pandianJihuaService.getById(detail.getDiskId());
    	if(!pandianJihua.getStatus().equals("未开始")) {
    		return ResponseData.error("该盘点计划已开盘！");
    	}
        this.pandianJihuaUserService.delete(pandianJihuaUserParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(PandianJihuaUserParam pandianJihuaUserParam) {
        PandianJihuaUser detail = this.pandianJihuaUserService.getById(pandianJihuaUserParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(PandianJihuaUserParam pandianJihuaUserParam,String condition ) {
        return this.pandianJihuaUserService.findPageBySpec(pandianJihuaUserParam,condition);
    }

}


